import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import('../components/Login')
const Home = () => import('../views/Home')
const WellCome = () => import('../views/wellcome')
const User = () => import('../views/user/user')
const GoodsList = () => import('../views/goods/goodsList')

Vue.use(VueRouter)

const routes = [{
	path: '',
	redirect: '/login'
},
{
	path: '/login',
	component: Login
},
{
	path: '/home',
	component: Home,
	redirect: '/wellcome',
	children: [
		{path: '/wellcome',component: WellCome},
		{path: '/users',component: User},
		{path: '/goods',component: GoodsList}
	]
}

]

const router = new VueRouter({
	routes
})
//路由导航守卫,判断用户是否登录，如果没有，不能通过url访问除login页以为的页面
router.beforeEach((to, from, next) => {
	if (to.path === '/login') return next();
	const tokenStr = window.sessionStorage.getItem('token');
	if (!tokenStr) return next('/login');
	next();
});

export default router
